#!/bin/bash
. ./var
. ./Tmpdir
Install_zabbix_server(){
    #定义zabbix server的IP地址
    zabbix_server=39.107.229.89
    zabbix_server_version="zabbix-4.4.7.tar.gz"
    TAR_zabbix_server_version="zabbix-4.4.7"
    if id zabbix &> /dev/null;then
        echoGreen "zabbix 用户已添加"
    else
	echo "即将添加zabbix用户"
	useradd -M -s /sbin/nologin zabbix
	[ -d /var/log/zabbix ] || mkdir /var/log/zabbix
	[ -d /etc/zabbix ] || mkdir /etc/zabbix
        [ -d /var/log/zabbix ] && chown zabbix.zabbix /var/log/zabbix/
    fi

    echoYellow "安装依赖包："
    yum install -y libevent-devel unixODBC-devel mysql-devel net-snmp-devel libxml2-devel libcurl-devel libevent-devel unixODBC-devel mysql-devel net-snmp-devel libxml2-devel libcurl-devel libevent-devel fping net-snmp-devel unixODBC-devel openssl-devel OpenIPMI-devel java-devel unixODBC-devel mysql-devel php-bcmath php-mbstring php-gd php-xml
    #调用Tmpdir函数
    Tmpdir
    cd $dir && wget -V &> /dev/null || yum -y install wget
    /usr/local/zabbix/sbin/zabbix_server --V &> /dev/null && echoRed "检测到系统中有zabbix_server命令，故而退出！" && rm -rf $dir && exit 1
    which zabbix_server &> /dev/null && echoRed "检测到系统中有zabbix_server命令，故而退出！" && rm -rf $dir && exit 1
    #wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.6-1.el7.x86_64.rpm
    wget $ip/${zabbix_server_version} && tar xf ./${zabbix_server_version} -C /usr/local
    [ -d /usr/local/${TAR_zabbix_server_version} ] && cd /usr/local/${TAR_zabbix_server_version} && ./configure --prefix=/usr/local/zabbix --sysconfdir=/etc/zabbix --enable-server --enable-agent --enable-proxy --enable-java --enable-ipv6 --with-mysql --with-net-snmp --with-libcrul --with-libxm12  --with-openipmi --with-unixodbc --with-openssl && make && make install 
    if [ $? = 0 ];then
        echoGreen "编译成功，进行配置zabbix"
        #配置环境变量
        echo 'export PATH=/usr/local/zabbix/bin:/usr/local/zabbix/sbin:$PATH' >> /etc/profile
        source /etc/profile  || . /etc/profile
	#配置数据库(让zabbix可以访问mysql)
	mysql -uroot -p666666 -e "create database zabbix character set utf8 collate utf8_bin;"
	mysql -uroot -p666666 -e "CREATE DATABASE zabbix_proxy character set utf8 collate utf8_bin;"
        mysql -uroot -p666666 -e "show databases;" | grep "zabbix"
        if [ $? = 0 ];then
	    echoGreen "zabbix数据库创建成功，下面进行授权"
            mysql -uroot -p666666 -e "grant all privileges on zabbix.* to zabbix@localhost identified by '666666';"
	    mysql -uroot -p666666 -e "GRANT ALL PRIVILEGES ON zabbix_proxy.* TO 'zabbix_proxy'@'localhost' identified by '666666';"
	    mysql -uroot -p666666 -e "flush privileges;"
	    echoYellow "请手动验证zabbix用户能登录mysql"
	    sleep 6
	    echoYellow "请手动写一个基于zabbix用户登陆mysql而且php能连接mysql："
            #导入zabbix数据,注意顺序
	    mysql -u root -p666666 zabbix < /usr/local/${TAR_zabbix_server_version}/database/mysql/schema.sql
            if [ $? = 0 ];then
	        mysql -u root -p666666 zabbix < /usr/local/${TAR_zabbix_server_version}/database/mysql/images.sql
                if [ $? = 0 ];then
	            mysql -u root -p666666 zabbix < /usr/local/${TAR_zabbix_server_version}/database/mysql/data.sql && mysql -u root -p666666 -e " flush privileges;" && echoGreen "zabbix数据导入成"
		    if [ $? = 0 ];then
	                echoYellow "修改Zabbix Server配置文件"
			sed -i.bak 's/# DBHost=localhost/DBHost=localhost/' /etc/zabbix/zabbix_server.conf
			#DBName与DBUser默认已更改
			#sed -i 's/DBName=zabbix/DBName=zabbix/' /etc/zabbix/zabbix_server.conf
                        #sed -i 's/DBUser=zabbix/DBUser=zabbix/' /etc/zabbix/zabbix_server.conf
                        sed -i '/^DBUser=zabbix/a\DBPassword=666666' /etc/zabbix/zabbix_server.conf
			sed -i '/^DBUser=zabbix/a\DBPort=3306' /etc/zabbix/zabbix_server.conf
			sed -i '/^DBUser=zabbix/a\DBSocket=/tmp/mysql.sock' /etc/zabbix/zabbix_server.conf
			NUM=`egrep "^DBHost|^DBName|^DBUser|^DBPassword|^DBPort|^DBSocket" /etc/zabbix/zabbix_server.conf | wc -l`
			#if [ ${NUM} == 6 ];then
			if [ ${NUM} -eq 6 ];then
		            echoGreen "zabbix server配置文件已完成"
			    echoGreen "即将复制zabbix web数据"
			    cp -rf /usr/local/${TAR_zabbix_server_version}/frontends/php/ /usr/local/nginx/html/zabbix
                            [ ! -d /usr/local/nginx/html/zabbix ] && echoGreen "zabbix web数据复制失败，即将退出" && rm -rf $dir && exit 16
			    sleep 6
			    echoGreen "zabbix web数据复制成功"
			    #启动脚本Zabbix,拷贝zabbix服务启动脚本：
			    echoYellow "#启动脚本Zabbix,拷贝zabbix服务启动脚本"
			    cp /usr/local/${TAR_zabbix_server_version}/misc/init.d/fedora/core/zabbix_* /etc/init.d/
			    [ -f /etc/init.d/zabbix_server ] && chmod 755 /etc/init.d/zabbix_*
                            [ -f /etc/init.d/zabbix_server ] && sed -i 's#BASEDIR=/usr/local#BASEDIR=/usr/local/zabbix#'  /etc/init.d/zabbix_server
                            [ -f /etc/init.d/zabbix_agentd ] && sed -i 's#BASEDIR=/usr/local#BASEDIR=/usr/local/zabbix#'  /etc/init.d/zabbix_agentd
                            echoGreen "启动脚本配置完成"

                            #解决报错zabbix_server --V   ----->zabbix_server: error while loading shared libraries: libmysqlclient.so.20: cannot open shared object file: No such file or directory
                            #参考： https://www.cnblogs.com/chxmtl/p/12618584.html
			    [ -f /usr/local/mysql/lib/libmysqlclient.so.20 ] && ln -s /usr/local/mysql/lib/libmysqlclient.so.20 /usr/lib64/
                            
			    find / -name  libmysqlclient.so.20
                            echoYellow "该文件需要做链接"

			    #删除临时目录
			    [ -d $dir ] && rm -rf $dir 



			    #修改zabbix数据文件
			    #[ -f /usr/local/nginx/html/zabbix/conf/zabbix.conf.php.example ] && cp /usr/local/nginx/html/zabbix/conf/zabbix.conf.php.example /usr/local/nginx/html/zabbix/conf/zabbix.conf.php
                            echoYellow "修改/usr/local/nginx/html/zabbix/conf/zabbix.conf.php"
                            wget -O  /usr/local/nginx/html/zabbix/conf/zabbix.conf.php https://download.gobgm.com/file/All_conf/zabbix.conf.php

			    ##启动zabbix_server
			    /etc/init.d/zabbix_server start && /etc/init.d/zabbix_server restart

			    #查看端口
			    netstat -nutlp | grep 10051
                              
			    #开机自启动
			    chkconfig --add zabbix_server
			    chkconfig --add zabbix_agentd
			    chkconfig zabbix_server on
			    chkconfig zabbix_agentd on
			    chkconfig

			else
		            echoRed "zabbix server配置文件修改失败，即将退出"
			    [ -d $dir ] && rm -rf $dir && exit 13
			fi
                    else
			echoRed "zabbix数据库导入失败" && rm -rf $dir && exit 14
                    fi
		else
		    echoRed "导入数据失败"
		    [ -d $dir ] && rm -rf $dir && exit 34
		fi
	    else
	        echoRed "导入数据失败，即将退出"
		[ -d $dir ] && rm -rf $dir && exit 35
	    fi
              

        else
	    echoRed "创建数据库zabbix失败，即将退出"
            [ -d $dir ] && rm -rf $dir && exit 16
	fi
    else
	echoRed "编译安装失败，即将退出" && rm -rf $dir && exit 7
    fi
}
